﻿using pixeleditor.WorkingSpace.Resource;
using spriteeditor;
using spriteeditor.Renderer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ttbit.core;
using ttbit.WorkingSpace;
using static System.Net.Mime.MediaTypeNames;

namespace pixeleditor.Editor
{
    public class Editor_Sprite : IWorkResourceEditor
    {
        public string TypeName => Resource_Sprite.TypeStr;

        Resource_Sprite sprite;
        Texture texture;
        int scale = 1; //保存scale
        Label uiScaleLabel;
        TrackBar uiTrackBar;
        public void OnActive(EditorApp app, IWorkResource resource)
        {
            sprite = resource as Resource_Sprite;
            //if (sprite.sprite.inPack)
            //{
            //    texture = (app.resmgr.GetResource(sprite.sprite.textureName).Value as Resource_Texture).texture;
            //}
            //else
            {
                texture = sprite.textureStandalone;
            }
            if (texture == null)
            {
                return;
            }
            //设置Instpect功能
            {
                app.EditorAreaInspector.Controls.Add(new Label() { Text = "Width=" + texture.Width, AutoSize = true });
                app.EditorAreaInspector.Controls.Add(new Label() { Text = "Height=" + texture.Height, AutoSize = true });
                var btn = new Button() { Text = "切边", AutoSize = true };
                btn.Click += (s, e) =>
                {
                    //if (sprite.sprite.inPack)
                    //{
                    //    MessageBox.Show("只有独立sprite可以切边");
                    //    return;
                    //}
                    app.workingSpace.GetResourceInfo(resource.Name, out string path, out MetaInfo meta);
                    using (var fs = System.IO.File.OpenRead(path))
                    {
                        var t = CoreLoadTool.LoadTextureData_RGBA(fs, out var w, out var h);
                        CoreLoadTool.GetBorder(t, w, h, out var x1, out var y1, out var x2, out var y2);
                        var pv = sprite.Povit;
                        var olduv = sprite.uv;
                        pv.X -= (x1 - olduv.X);
                        pv.Y -= (y1 - olduv.Y);
                        sprite.uv = new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
                        sprite.Povit = pv;

                    }

                };
                app.EditorAreaInspector.Controls.Add(btn);
            }
            //设置编辑功能
            {
                var line = new FlowLayoutPanel() { FlowDirection = FlowDirection.LeftToRight, AutoSize = true };
                app.EditorAreaFunction.Controls.Add(line);


                //app.EditorAreaInspector;

                uiScaleLabel = new Label() { Text = "Scale", Width = 128, Height = 24 };
                uiTrackBar = new TrackBar() { AutoSize = true, Maximum = 16, Minimum = 1, Value = scale };
                uiTrackBar.ValueChanged += (s, e) =>
                {
                    scale = uiTrackBar.Value;
                    uiScaleLabel.Text = "Scale X" + scale;
                };
                uiScaleLabel.Text = "Scale X" + scale;

                line.Controls.Add(uiScaleLabel);
                line.Controls.Add(uiTrackBar);
            }
            {
                var line = new FlowLayoutPanel() { FlowDirection = FlowDirection.LeftToRight, AutoSize = true };
                app.EditorAreaFunction.Controls.Add(line);
                {
                    var btn = new Button() { Text = "放回左上角", AutoSize = true };
                    line.Controls.Add(btn);
                    btn.Click += (s, e) =>
                    {
                        this.posx = 16;
                        this.posy = 16;
                    };
                }
                var label = new Label() { Text = "功能：移动位置", Width = 160 };
                line.Controls.Add(label);
                {
                    var btn = new Button() { Text = "移动位置", AutoSize = true };
                    line.Controls.Add(btn);
                    btn.Click += (s, e) =>
                    {
                        label.Text = "功能：移动位置";
                        editmode = 1;
                    };
                }

                {
                    var btn = new Button() { Text = "移动Povit", AutoSize = true };
                    line.Controls.Add(btn);
                    btn.Click += (s, e) =>
                    {
                        label.Text = "功能：移动Povit";
                        editmode = 2;
                    };
                }
            }

            posx = 16;
            posy = 16;
            pointdown = false;
            editmode = 1;
        }
        public void OnDeactive(EditorApp app)
        {
            this.uiScaleLabel = null;
            //if (!sprite.sprite.inPack)
            {
                this.texture?.Dispose();
            }
            this.uiTrackBar = null;
        }

        int editmode = 1;
        int posx = 16;
        int posy = 16;
        public void OnRender(TTRender2 graphics)
        {
            if (texture != null)
            {
                int x = posx;
                int y = posy;
                int x2 = x + sprite.sprite.Width * scale;
                int y2 = y + sprite.sprite.Height * scale;
                graphics.DrawTexture(x, y, x2, y2, texture, sprite.sprite.uv);
                graphics.DrawBorder(x, y, x2, y2, 1, ttbit.math.ColorF.FromARGB(1, 1, 1, 0));
                int crossx = x + sprite.sprite.povit.X * scale;
                int crossy = y + sprite.sprite.povit.Y * scale;
                graphics.DrawCross(crossx, crossy, 100, scale, ttbit.math.ColorF.FromARGB(1, 1, 1, 1));
            }
        }
        int dx = 0;
        int dy = 0;
        int oldx = 0;
        int oldy = 0;
        bool pointdown;
        public void OnPointDown(int id, int x, int y)
        {
            pointdown = true;
            dx = x;
            dy = y;
            if (editmode == 1)//拖动
            {
                this.oldx = this.posx;
                this.oldy = this.posy;
            }
            if (editmode == 2)//移轴
            {
                oldx = this.sprite.Povit.X;
                oldy = this.sprite.Povit.Y;
            }
        }
        public void OnPointMove(int id, int x, int y)
        {
            if (pointdown)
            {
                var mx = x - dx;
                var my = y - dy;
                if (editmode == 1)//拖动
                {
                    this.posx = oldx + mx;
                    this.posy = oldy + my;
                }
                if (editmode == 2)//移轴
                {
                    var p = new Point(mx);
                    p.X = oldx + mx / scale;
                    p.Y = oldy + my / scale;
                    this.sprite.Povit = p;
                }


            }
        }
        public void OnPointUp(int id, int x, int y)
        {
            pointdown = false;
        }
        public void OnPropChange()
        {

        }
    }
}
